package com.kob.backend.controller.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.DueHistoryMapper;
import com.kob.backend.pojo.DueHistroy;
import com.kob.backend.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class UserDueHistory {
    @Autowired
    private DueHistoryMapper dueHistoryMapper;

    /*
     * 获取用户历史预约记录
     */
    @GetMapping("/user/account/getDueHistory")
    public List<DueHistroy> getDueHistory(HttpServletRequest request) {
        //检查token
        String token = request.getHeader("Authorization");
        if (null == token || token.trim().isEmpty()) {
            System.out.println("[ERROR] token is error");
        }
        // 获取userid
        String userid = JwtUtil.extractUsername(token.replace("Bearer","").trim());
        // 查询数据库中对应userid的duehistory记录
        QueryWrapper<DueHistroy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid)
                .orderByDesc("duetime");
        // 返回查询结果给前端
        return dueHistoryMapper.selectList(queryWrapper);
    }
}
